trigger WriteInStoreVisitBySSYToDashboard on In_Store_Visit__c (after insert) {
    List<TradeDashboardDetails__c> ssyList=new List<TradeDashboardDetails__c>();
    ScoreStandard__c score=ScoreStandard__c.getValues('SSY Activity Achievement');
    List<String> MonthList=new List<String>();
    List<String> YearList=new List<String>();
    List<Date> DateList=new List<Date>();
    List<Id> lastActual=new List<Id>();
    List<Date> lastDate=new List<Date>();
    
    String strM='',strY='';
    for(In_Store_Visit__c st:trigger.new)
    {
        DateTime dt=st.Start_Time__c;
        Date d=Date.newInstance(dt.year(),dt.month(),1);
        MonthList.add(String.valueof(dt.month()));
        YearList.add(String.valueOf(dt.year()));
        DateList.add(d);
        lastActual.add(st.OutletName_del__c);
        
        lastDate.add(d.addMonths(-1));
    }
    
    List<Sales_Target__c> targetList=[select TotalValue__c,Month__c,Year__c,NextSales__c,Sales_Target_Type__c from Sales_Target__c 
                                        where NextSales__c=:UserInfo.getUserId() and Month__c in: MonthList
                                        and Year__c in:YearList and Sales_Target_Type__c='SSY Activity Outlet Number'];
    
   
    List<TradeDashboardDetails__c> inSvList=[select Outlet__c,SalesMonth__c
     from TradeDashboardDetails__c where OwnerId=:UserInfo.getUserId() and SalesMonth__c in:DateList and CriticalEnabler__c='SSY Activity Achievement'];
     
     
     List<TradeDashboardDetails__c> getLastMonthData=[select Outlet__c,Actual__c from TradeDashboardDetails__c where OwnerId=:UserInfo.getUserId() and SalesMonth__c in:lastDate and CriticalEnabler__c='SSY Activity Achievement' and Outlet__c in:lastActual];
     Map<Id,String> lastActualMap=new Map<Id,String>();
     //取出上个月的执行次数加入到map中
     if(getLastMonthData !=NULL && getLastMonthData.size()>0)
     {
        for(TradeDashboardDetails__c t:getLastMonthData)
        {
            if(lastActualMap.containsKey(t.Outlet__c))
            {
                Integer i=Integer.valueof(lastActualMap.get(t.Outlet__c));
                lastActualMap.put(t.Outlet__c,String.valueOf(i+Integer.valueOf(t.Actual__c)));
            }
            else
            {
                //trigger.new[0].addError(String.valueof(t.Actual__c));
                if(t.Actual__c!=NULL)
                    lastActualMap.put(t.Outlet__c,String.valueOf(t.Actual__c));
                else
                    lastActualMap.put(t.Outlet__c,'0');
            }
        }
     }
     else
     {
        for(In_Store_Visit__c st:trigger.new)
        {
            lastActualMap.put(st.OutletName_del__c,'0');
        }
     }
     
     Map<Id,Date> mapList=new Map<Id,Date>();
     Map<Date,String> mapList2=new Map<Date,String>();
     
     if(targetList.size()>0)//写入SalesTarget到map中
     {
         for(Sales_Target__c i:targetList)
         {
            Date d=Date.newInstance(Integer.valueOf(i.year__c),Integer.valueOf(i.Month__c),1);
            //trigger.new[0].addError(i.Month__c+'dddd'+String.valueof(i.TotalValue__c));
            mapList2.put(d,String.valueof(i.TotalValue__c));
         }
     }
     else
     {
        for(In_Store_Visit__c st:trigger.new)
        {
            DateTime dt=st.Start_Time__c;
            Date d=Date.newInstance(dt.year(),dt.month(),1);
            mapList2.put(d,String.valueof(0));
        }
     }
         
     if(inSvList!=NULL && inSvList.size()>0)
     {
         for(TradeDashboardDetails__c ssy:inSvList)
         {
            mapList.put(ssy.Outlet__c,ssy.SalesMonth__c);
         }
         
         for(In_Store_Visit__c isvisit:trigger.new)
        {
            if(isvisit.SSYActivityAchievement__c)
            {
                if(!mapList.containsKey(isvisit.OutletName_del__c))
                {
                    DateTime dt=isvisit.Start_Time__c;
                    Date d=Date.newInstance(dt.year(),dt.month(),1);
                    Integer TotalNum=Integer.valueof(mapList2.get(d));
                    
                    Integer lastActualTotal=Integer.valueOf(lastActualMap.get(isvisit.OutletName_del__c));
                    
                    TradeDashboardDetails__c sae=new TradeDashboardDetails__c();
                    sae.CriticalEnabler__c='SSY Activity Achievement';
                    sae.Outlet__c=isvisit.OutletName_del__c;
                    sae.ScoreStandard__c=score.Standard__c;
                    sae.LastMonthActual__c=lastActualTotal;
                    sae.Actual__c=1;
                    
                    if(inSvList!=NULL && inSvList.size()>0)//如果同一个TSR在TradeDashboardDetails__c里有值说明已经存入了SalesTarget
                        sae.Target__c=0;
                    else
                        sae.Target__c=TotalNum;
                    
                    sae.OwnerId=UserInfo.getUserId();
                    sae.SalesMonth__c=d;
                    ssyList.add(sae);
                    //sae
                }
            }
        }
     }
     else
     {
        for(In_Store_Visit__c isvisit:trigger.new)
        {
            if(isvisit.SSYActivityAchievement__c)
            {
                DateTime dt=isvisit.Start_Time__c;
                Date d=Date.newInstance(dt.year(),dt.month(),1);
                Integer TotalNum=Integer.valueof(mapList2.get(d));
                //trigger.new[0].addError(String.valueOf(lastActualMap.size()));
                Integer lastActualTotal=Integer.valueOf(lastActualMap.get(isvisit.OutletName_del__c));
                
                TradeDashboardDetails__c sae=new TradeDashboardDetails__c();
                sae.CriticalEnabler__c='SSY Activity Achievement';
                sae.Outlet__c=isvisit.OutletName_del__c;
                sae.ScoreStandard__c=score.Standard__c;
                sae.LastMonthActual__c=lastActualTotal;
                sae.Actual__c=1;
                if(inSvList!=NULL && inSvList.size()>0)//如果同一个TSR在TradeDashboardDetails__c里有值说明已经存入了SalesTarget
                    sae.Target__c=0;
                else
                    sae.Target__c=TotalNum;
                
                sae.OwnerId=UserInfo.getUserId();
                sae.SalesMonth__c=d;
                ssyList.add(sae);
            }
        }
     }
     
     insert ssyList;
     
     
/*以下是SSYActivityExecutionTracking__c逻辑********************************************************************************/

 List<SSYActivityExecutionTracking__c> ssyActivityList=new List<SSYActivityExecutionTracking__c>();
   
   // List<Date> DateList=new List<Date>();
    
   // String strM='',strY='';
   // for(In_Store_Visit__c st:trigger.new)
   // {
   //     Date d=Date.newInstance(dt.year(),dt.month(),1);
   //     DateList.add(d);
   // }
    
    //String sql='select TotalValue__c,Month__c,Year__c,NextSales__c,Sales_Target_Type__c from Sales_Target__c where NextSales__c=\''+UserInfo.getUserId()+'\' and ('+strM+') and ('+strY+') and Sales_Target_Type__c=\'SSY Activity Outlet Number\'';
   
    //List<Sales_Target__c> targetList=DataBase.query(sql);
   
    List<SSYActivityExecutionTracking__c> inSSYSvList=[select OutletName__c,SalesMonth__c,SSYTarget__c
     from SSYActivityExecutionTracking__c where OwnerId=:UserInfo.getUserId() and SalesMonth__c in:DateList];
     
     Map<Id,Date> ExecutMapList=new Map<Id,Date>();
     Map<Date,String> SSymapList=new Map<Date,String>();
     
     if(targetList.size()>0)//写入SalesTarget
     {
         for(Sales_Target__c i:targetList)
         {
            Date d=Date.newInstance(Integer.valueOf(i.year__c),Integer.valueOf(i.Month__c),1);
            //trigger.new[0].addError(i.Month__c+'dddd'+String.valueof(i.TotalValue__c));
            SSymapList.put(d,String.valueof(i.TotalValue__c));
            //trigger.new[0].addError(m);
         }
     }
     else
     {
        for(In_Store_Visit__c st:trigger.new)
        {
            DateTime dt=st.Start_Time__c;
            Date d=Date.newInstance(dt.year(),dt.month(),1);
            SSymapList.put(d,String.valueof(0));
        }
     }
         
     if(inSSYSvList!=NULL && inSSYSvList.size()>0)
     {
         for(SSYActivityExecutionTracking__c ssy:inSSYSvList)
         {
            ExecutMapList.put(ssy.OutletName__c,ssy.SalesMonth__c);
         }
         
         for(In_Store_Visit__c isvisit:trigger.new)
        {
            if(isvisit.SSYActivityAchievement__c)
            {
                if(!ExecutMapList.containsKey(isvisit.OutletName_del__c))
                {
                    DateTime dt=isvisit.Start_Time__c;
                    Date d=Date.newInstance(dt.year(),dt.month(),1);
                    Integer TotalNum=Integer.valueof(SSymapList.get(d));
                    
                    SSYActivityExecutionTracking__c sae=new SSYActivityExecutionTracking__c();
                    sae.OutletName__c=isvisit.OutletName_del__c;
                    sae.ExecutedSSY__c=1;
                    if(inSvList!=NULL && inSvList.size()>0)//如果同一个TSR在SSYActivityExecutionTracking__c里有值说明已经存入了SalesTarget
                        sae.SSYTarget__c=0;
                    else
                        sae.SSYTarget__c=TotalNum;
                    
                    //sae.TSR__c=UserInfo.getUserId();
                    sae.SalesMonth__c=Date.newInstance(dt.year(),dt.month(),1);
                    sae.OwnerId=UserInfo.getUserId();
                    ssyActivityList.add(sae);
                    //sae
                }
            }
        }
     }
     else
     {
        for(In_Store_Visit__c isvisit:trigger.new)
        {
            if(isvisit.SSYActivityAchievement__c)
            {
                DateTime dt=isvisit.Start_Time__c;
                //trigger.new[0].addError(String.valueof(dt.month()));
                Date d=Date.newInstance(dt.year(),dt.month(),1);
                Integer TotalNum=Integer.valueof(SSymapList.get(d));
                //Integer TotalNum=Integer.valueof(mapList2.get(String.valueOf(dt.month())));
                    
                SSYActivityExecutionTracking__c sae=new SSYActivityExecutionTracking__c();
                sae.OutletName__c=isvisit.OutletName_del__c;
                sae.ExecutedSSY__c=1;
                sae.SSYTarget__c=TotalNum;
               // sae.TSR__c=UserInfo.getUserId();
                sae.SalesMonth__c=Date.newInstance(dt.year(),dt.month(),1);
                sae.OwnerId=UserInfo.getUserId();
                ssyActivityList.add(sae);
            }
        }
     }
     
     insert ssyActivityList;

}